<template>
  <div class="group-emotion-container">
    <h2>群体情绪状态</h2>

    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="search-form mb-6">
      <div class="form-row">
        <el-form-item label="学号" class="form-item">
          <el-input v-model="searchForm.studentId" placeholder="请输入学号" clearable />
        </el-form-item>
        <el-form-item label="姓名" class="form-item">
          <el-input v-model="searchForm.name" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="班主任" class="form-item">
          <el-input v-model="searchForm.teacher" placeholder="请输入班主任" clearable />
        </el-form-item>
      </div>

      <div class="form-row">
        <el-form-item label="班级" class="form-item">
          <el-select
            v-model="searchForm.class"
            placeholder="请选择班级"
            clearable
          >
            <el-option
              v-for="item in classOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="风险状态" class="form-item">
          <el-select v-model="searchForm.riskStatus" placeholder="请选择状态" clearable>
            <el-option label="正常" value="normal" />
            <el-option label="低风险" value="low" />
            <el-option label="中风险" value="medium" />
            <el-option label="高风险" value="high" />
          </el-select>
        </el-form-item>

        <el-form-item class="form-item action-buttons">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </div>
    </el-form>

    <!-- 表格 -->
    <el-card shadow="never">
      <el-table :data="filteredTableData" border style="width: 100%">
        <el-table-column prop="studentId" label="学号" width="120" />
        <el-table-column prop="name" label="姓名" width="100" />
        <el-table-column prop="class" label="班级" width="120" />
        <el-table-column prop="teacher" label="班主任" width="100" />
        <el-table-column prop="teacherPhone" label="班主任联系方式" width="120" />
        <el-table-column prop="emotion" label="情绪状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getEmotionTagType(row.emotion)">
              {{ row.emotion }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="riskStatus" label="风险状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getRiskTagType(row.riskStatus)">
              {{ getRiskText(row.riskStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
            <el-button size="small" type="primary" @click="handleEnter(row)">进入</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
          <div class="pagination-container">
            <el-pagination
              background
              :current-page="currentPage"
              :page-sizes="pageSizes"
              :page-size="pageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
      </div>
    </el-card>



    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="编辑情绪状态" width="50%">
      <el-form :model="editForm" label-width="120px">
        <el-form-item label="情绪状态">
          <el-select v-model="editForm.emotion" placeholder="请选择情绪状态">
            <el-option label="高兴" value="happy" />
            <el-option label="悲伤" value="sad" />
            <el-option label="愤怒" value="angry" />
            <el-option label="惊讶" value="surprise" />
            <el-option label="恐惧" value="fear" />
            <el-option label="厌恶" value="disgust" />
          </el-select>
        </el-form-item>
        <el-form-item label="风险状态">
          <el-select v-model="editForm.riskStatus" placeholder="请选择风险状态">
            <el-option label="正常" value="normal" />
            <el-option label="低风险" value="low" />
            <el-option label="中风险" value="medium" />
            <el-option label="高风险" value="high" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmEdit">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

// 表格数据
const tableData = ref([
  {
    studentId: '2023001',
    name: '张',
    class: '计算机1班',
    teacher: '李老师',
    teacherPhone: '13800138002',
    emotion: 'happy',
    riskStatus: 'normal'
  },
  {
    studentId: '2023002',
    name: '李四',
    class: '计算机2班',
    teacher: '王老师',
    teacherPhone: '13800138003',
    emotion: 'sad',
    riskStatus: 'low'
  },
  {
    studentId: '2023003',
    name: '王五',
    class: '计算机3班',
    teacher: '赵老师',
    teacherPhone: '13800138004',
    emotion: 'angry',
    riskStatus: 'medium'
  },
  {
    studentId: '2023004',
    name: '赵六',
    class: '计算机4班',
    teacher: '钱老师',
    teacherPhone: '13800138005',
    emotion: 'fear',
    riskStatus: 'high'
  }
])

// 班级选项
const classOptions = ref([
  { value: '计算机1班', label: '计算机1班' },
  { value: '计算机2班', label: '计算机2班' },
  { value: '计算机3班', label: '计算机3班' },
  { value: '计算机4班', label: '计算机4班' }
])

// 搜索表单
const searchForm = ref({
  studentId: '',
  name: '',
  class: '',
  teacher: '',
  riskStatus: ''
})

// 分页
const pageSizes = ref([10, 20, 30, 50])
const total = ref(4)  // 改为实际数据条数
const pageSize = ref(10)
const currentPage = ref(1)

// 编辑对话框
const dialogVisible = ref(false)
const editForm = ref({
  emotion: '',
  riskStatus: ''
})
let editingRow = null

// 过滤后的表格数据
const filteredTableData = ref([...tableData.value])

// 方法
const handleSearch = () => {
  const { studentId, name, class: classValue, teacher, riskStatus } = searchForm.value;
  filteredTableData.value = tableData.value.filter(item => {
    return (
      (!studentId || item.studentId.includes(studentId)) &&
      (!name || item.name.includes(name)) &&
      (!classValue || item.class.includes(classValue)) &&
      (!teacher || item.teacher.includes(teacher)) &&
      (!riskStatus || item.riskStatus === riskStatus)
    );
  });
  total.value = filteredTableData.value.length; // 更新总条数
}

const resetSearch = () => {
  searchForm.value = {
    studentId: '',
    name: '',
    class: '',
    teacher: '',
    riskStatus: ''
  }
  filteredTableData.value = [...tableData.value]
  total.value = filteredTableData.value.length;
}

const handleEdit = (row) => {
  editingRow = row
  editForm.value = {
    emotion: row.emotion,
    riskStatus: row.riskStatus
  }
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定删除该记录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = tableData.value.findIndex(item => item.studentId === row.studentId)
    if (index !== -1) {
      tableData.value.splice(index, 1)
      filteredTableData.value = [...tableData.value]
      total.value = filteredTableData.value.length
      ElMessage.success('删除成功')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleEnter = (row) => {
  router.push({name: 'StudentDetail', params: {id: row.studentId}})
}

const confirmEdit = () => {
  if (editingRow) {
    editingRow.emotion = editForm.value.emotion
    editingRow.riskStatus = editForm.value.riskStatus
    ElMessage.success('修改成功')
    dialogVisible.value = false
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
}

const handleCurrentChange = (val) => {
  currentPage.value = val
}

// 辅助方法
const getEmotionTagType = (emotion) => {
  const map = {
    happy: 'success',
    sad: 'info',
    angry: 'danger',
    surprise: 'warning',
    fear: '',
    disgust: 'warning'
  }
  return map[emotion] || ''
}

const getRiskTagType = (riskStatus) => {
  const map = {
    normal: 'success',
    low: '',
    medium: 'warning',
    high: 'danger'
  }
  return map[riskStatus] || ''
}

const getRiskText = (riskStatus) => {
  const map = {
    normal: '正常',
    low: '低风险',
    medium: '中风险',
    high: '高风险'
  }
  return map[riskStatus] || ''
}
</script>

<style scoped>
.group-emotion-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.form-row {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.form-item {
  margin-right: 16px;
  margin-bottom: 0;
}

.form-item :deep(.el-form-item__label) {
  width: 80px;
  text-align: right;
}

.form-item :deep(.el-input),
.form-item :deep(.el-select) {
  width: 180px;
}

.action-buttons {
  margin-left: auto;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>